package com.example.tests;

import static org.testng.Assert.*;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.example.fw.AccountHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SingupTest extends TestBase {
	   public User user = new User().setLogin("tanua_siv20").setEmail("tanua_siv20@localhost.localdomain").setPassword("123456");
	   public User administrator = new User().setLogin("administrator").setPassword("root");
	   private JamesHelper james;
	   private AccountHelper accHelper;
	   
	   @BeforeClass
	   public void createMailUser(){
		   james = app.getJamesHelper();
		   accHelper = app.getAccountHelper();
		   if (! james.doesUserExist(user.login)){
			   james.createUser(user.login, user.password);
		   }
	   }
	   
	   @AfterClass
	   public void deleteMailUser(){
		   if (james.doesUserExist(user.login)){
			   james.deleteUser(user.login);
		   }
	   }

	   @Test
	   public void newUserShouldSignup(){
			accHelper.signup(user);
			accHelper.login(user);
			assertThat(accHelper.loggedUser(), equalTo(user.login));
			accHelper.verifySignup(user, administrator);
			accHelper.deleteUser(user);
			
		}
		
	   //@Test 
	   public void existingUserShouldNotSignup(){
		   
		   try{
			   accHelper.signup(user);  
		   } catch(Exception e) {  
			   assertThat(e.getMessage(), containsString("That username is already being used. Please go back and select another one."));
			   return;   
		   }
		   fail("Exception excepted");
	   }


}
